<?php
declare(strict_types=1);

namespace App\Admin\Controller;
use App\Common\Controller\AbstractController;
use App\Common\Logic\FileLogic;
use App\Common\Validate\Validate;
use App\Server\Model\Admin;
use App\Server\Model\AdminGroup;
use App\Server\Model\AdminMenu;
use Hyperf\Di\Annotation\Inject;
use App\Admin\Validate\IndexValidate;
use Hyperf\Utils\Context;
use Ueditor\Ueditor;

class IndexController extends AbstractController
{

    /**
     * @Inject()
     * @var IndexValidate
     */
    protected $IndexValidate;

    public function index()
    {
        $menu_id=$this->request->query('menu_id');
        if (!empty($menu_id)&&$menu_id>0){
            $this->session->set('admin_top_menu_id',$menu_id);
        }else{
            $this->session->set('admin_top_menu_id',0);
        }
        $data = [];
        $data['uInfo'] = Context::get('adminInfo');
        $header_menu = $this->menulist();
        $data['header_menu'] = get_top_menu($header_menu);
        return $this->render->render('/admin/index/index',$data);
    }

    public function console()
    {
        return $this->render->render('/admin/index/console');
    }

    public function getadminmenulist()
    {
        $admin_top_menu_id = $this->session->get('admin_top_menu_id');
        if ($admin_top_menu_id>0){
            $uRulesMenu = $this->menulist();
            $uRulesMenu = list_to_tree($uRulesMenu, $admin_top_menu_id);
        }else{
            $uRulesMenu = [];
        }
        return $this->success($uRulesMenu);
    }

    public function upload(){
        $uploadLogic = new FileLogic();
        if ($this->request->hasFile('file')) {
            if ($this->request->file('file')->isValid()) {
                $file = $this->request->file('file');
                $getRes = $uploadLogic->uploadFile($file, 'admin');
                if ($getRes['state']){
                    return $this->success($getRes['data']['filePath'],'上传成功');
                }
            }
        }
        return $this->error('上传失败');
    }


    public function uploadByEdit(){
        $uploadLogic = new FileLogic();
        if ($this->request->hasFile('file')) {
            if ($this->request->file('file')->isValid()) {
                $file = $this->request->file('file');
                $getRes = $uploadLogic->uploadFile($file, 'edit');
                if ($getRes['state']){
                    return $this->success(['src'=>$getRes['data']['filePath']],'上传成功');
                }

//                $res = [
//                    'resourceType'=>'Files',
//                    'currentFolder'=>[
//                        'path'=>'/',
//                        'url' =>'/uploads/edit/G8baf9753667d7047863a30166a9d5514/',
//                        'acl' =>255
//                    ],
//                    'fileName'=>'201808270633405540.jpg',
//                    'uploaded'=>1
//                ];
//                return $res;
            }
        }
        return $this->error('上传失败');
    }




    public function getRuleList()
    {
        $AdminMenu = new AdminMenu();
        $list = $AdminMenu->getList([],['id','pid','title','is_menu']);
        // 配合layui树形组件提供的额外数据
        foreach ($list as $k => $v) {
            // 默认全部展开
            // $list[$k]['spread'] = true;
            // 标注菜单和纯权限节点
            $v['is_menu'] == 1?$list[$k]['title'] = '<span class="layui-badge">权限</span> <span class="layui-badge layui-bg-blue">菜单</span> ' . $v['title']:$list[$k]['title'] = '<span class="layui-badge">权限</span> ' . $v['title'];
        }
        return $this->success(list_to_tree($list));
    }



    protected function menulist(){
        $adminInfo = Context::get('adminInfo');
        //用户所在权限组及所拥有的权限
        $adminMenu = new AdminMenu();
        $adminModel = new Admin();
        $where=[];
        $where[]=['is_menu','=',1];
        $uRulesMenu = $adminMenu -> getCacheList($where,'display_menu');
        if($adminInfo['id'] != 1)
        {
            $adminGroup = new AdminGroup();
            $uGroupIds = explode(',',$adminModel->getValue([['id','=',$adminInfo['id']]],'groups'));
            $where = [];
            $where[] = ['status','=',1];
            $where['whereIn'][] = ['id',$uGroupIds];
            $uRuleIds  = $adminGroup->getList($where,['rules']);
            $uRules = [];
            foreach ($uRuleIds as $k => $v) {
                if (!empty($v['rules'])){
                    $uRules = array_unique(array_merge($uRules,explode(',',$v['rules'])));
                }
            }
        }
        foreach ($uRulesMenu as $k => &$v) {
            if ($adminInfo['id'] != 1&&!in_array($v['id'],$uRules)){
                unset($uRulesMenu[$k]);
                continue;
            }
            if(!empty($v['src'])) {
                $uRulesMenu[$k]['href'] = (string)url($v['src']);
            }
        }

        // 在菜单列表的开头添加首页
        array_unshift($uRulesMenu,['id'=>-1,'pid'=>0,'title'=>'首页','href'=>'','fontFamily'=>'','icon'=>'layui-icon-home']);
        return $uRulesMenu;
    }

}
